<?php

namespace app\repository\dao\api;

use support\Db;

class OrderDao
{
    public static function getList(array &$data): array
    {
        $fields = [
            'id as receipt_id', 'sales_num as order_id', 'orgz_id', 'orgz_name', 'genre', 'mode', 'ought_amount',
            'actual_amount', 'total_amount', 'created_at', 'deal_begin_time', 'upload_at', 'deal_complete_at as completed_at',
            'return_doc_code as pre_order_id', 'client_code', 'assistant_id', 'assistant_name'
        ];
        $query = Db::table('product_sales')
            ->where('deal_status', 1)
            ->when($data['receipt_date_begin'], function ($query) use (&$data) {
                return $query->where('deal_complete_time', '>=', strtotime($data['receipt_date_begin']));
            })
            ->when($data['receipt_date_end'], function ($query) use (&$data) {
                return $query->where('deal_complete_time', '<=', strtotime($data['receipt_date_end']));
            });

        $total = $query->count();
        $list = $query->offset($data['start'])
            ->limit($data['length'])
            ->orderBy('upload_at', 'DESC')
            ->select($fields)
            ->get()->toArray();
        return ['total' => $total, 'list' => $list];
    }
}